package freemarkDemo;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class Test {

	public static void main(String[] args) throws Exception {
		//1.创建一个配置对象
		Configuration configuration = new Configuration(Configuration.getVersion());

		String root = System.getProperty("user.dir");

		//2.设置模版所在目录
		configuration.setDirectoryForTemplateLoading(new File(root + "/src/main/resources"));
	
		//3.设置字符集
		configuration.setDefaultEncoding("utf-8");
		
		//4.获取模版对象
		Template template =  configuration.getTemplate("test.ftl");
		
		//5.创建数据模型
		Map map = new HashMap();
		map.put("name", "张三");
		map.put("success", false);
		
		
		List goodsList=new ArrayList();
		Map goods1=new HashMap();
		goods1.put("name", "苹果");
		goods1.put("price", 5.8);
		Map goods2=new HashMap();
		goods2.put("name", "香蕉");
		goods2.put("price", 2.5);
		Map goods3=new HashMap();
		goods3.put("name", "橘子");
		goods3.put("price", 3.2);
		goodsList.add(goods1);
		goodsList.add(goods2);
		goodsList.add(goods3);
		
		map.put("goodsList", goodsList);

		map.put("today", new Date());
		
		map.put("point", 1231232123);
		//6.创建一个输出流对象
		Writer out = new FileWriter(root + "/out/a.html");
		
		//7.输出
		template.process(map, out);
		
		//8. 关闭流
		out.close();
	}
}
